<template>
  <div class="article-container">
    <!-- 导航栏 -->
    <van-nav-bar
      fixed
      left-arrow
      title="文章详情"
      @click-left="$router.back()"
    />
    <!-- /导航栏 -->

    <!-- 加载中 loading -->
    <van-loading v-if="loading" class="article-loading" />
    <!-- /加载中 loading -->

    <!-- 文章详情 -->
    <div class="detail">
      <h3 class="title">{{ articleDetail.title }}</h3>
      <div class="author">
        <van-image round width="1rem" height="1rem" fit="fill" :src="articleDetail.aut_photo" />
        <div class="text">
          <p class="name">{{ articleDetail.aut_name }}</p>
          <p class="time">{{ articleDetail.pubdate|fromNow }}</p>
        </div>
        <van-button
          round
          size="small"
          type="info"
          @click="clickIsFollow(articleDetail.is_followed)"
        >{{ articleDetail.is_followed?'取关':'关注' }}</van-button>
      </div>
      <div class="content">
        <div v-html="articleDetail.content">正文</div>
      </div>
      <van-divider>END</van-divider>
      <div class="zan">
        <van-button
          round
          size="small"
          hairline
          type="primary"
          plain
          :icon="articleDetail.attitude===1?'good-job': 'good-job-o'"
          @click="clickTag( articleDetail.attitude)"
        >
          {{ articleDetail.attitude===1?'取消点赞': '点赞' }}
        </van-button>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <van-button
          round
          size="small"
          hairline
          type="danger"
          plain
          :icon="articleDetail.attitude===-1?'delete': 'delete-o'"
          @click="clickLike( articleDetail.attitude)"
        >
          {{ articleDetail.attitude===-1?'取消不喜欢': '不喜欢' }}
        </van-button>
      </div>
    </div>
    <!-- /文章评论 -->
    <Comment v-if="!loading" :ariticle-id="articleDetail.art_id.toString()" />
  </div>
</template>

<script>
import { reqGetAriticlesDetail, reqUnfollow, reqFollow, reqDelLike, reqAddLike, reqDelDislike, reqAddDislike } from '@/api/articles'
import Comment from '@/views/article/components/comment.vue'
// push时传对象, params (写name传递) 就通过 this.$route.params获取
// push时传对象, query  (写path)     就通过 this.$route.query 获取
// 动态路由传参  /article/:id        this.$route.params 获取

export default {
  name: 'ArticleIndex',
  components: {
    Comment
  },
  data() {
    return {
      loading: true, // 控制加载中的 loading 状态
      article: {},
      articleDetail: {}
    }
  },
  async created() {
    const res = await reqGetAriticlesDetail(this.$route.params.id)
    this.articleDetail = res.data
    this.loading = !this.loading
  },
  methods: {
    async clickIsFollow(isFollow) {
      if (isFollow) {
        // 取关
        await reqUnfollow(this.articleDetail.aut_id)
      } else {
        // 关注

        await reqFollow(this.articleDetail.aut_id)
      }
      this.articleDetail.is_followed = !this.articleDetail.is_followed
    },
    async clickTag(attitude) {
      attitude === 1 ? await reqDelLike(this.articleDetail.art_id) : await reqAddLike(this.articleDetail.art_id)
      attitude === 1 ? this.articleDetail.attitude = 0 : this.articleDetail.attitude = 1
    },
    async clickLike(attitude) {
      attitude === -1 ? await reqAddDislike(this.articleDetail.art_id) : await reqDelDislike(this.articleDetail.art_id)
      attitude === -1 ? this.articleDetail.attitude = 0 : this.articleDetail.attitude = -1
    }
  }
}
</script>

<style lang='scss' scoped>
.article-container{
  position: absolute;
  left: 0;
  top: 0;
  overflow-y: scroll;
  width: 100%;
  height: 100%;
}
.article-loading {
  padding-top: 100px;
  text-align: center;
}
.error{
  padding-top: 100px;
  text-align: center;
}
.detail {
  padding: 50px 10px;
  .title {
    font-size: 16px;
  }
  .zan{
    text-align: center;
  }
  .author {
    padding: 10px 0;
    display: flex;
    .text {
      flex: 1;
      padding-left: 10px;
      line-height: 1.3;
      .name {
        font-size: 14px;
        margin: 0;
      }
      .time {
        margin: 0;
        font-size: 12px;
        color: #999;
      }
    }
  }
  .content {
    font-size:14px;
    overflow: hidden;
    white-space: pre-wrap;
    word-break: break-all;
    ::v-deep img{
      max-width:100%;
      background: #f9f9f9;
    }
  }
}
</style>
